home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
clean
/
sun3.lha
/
Sun3
/
seqdemos
/
fsieve.icl
< prev
next >
Wrap
Text File
|
1992-08-07
|
1KB
|
52 lines
MODULE fsieve;
<<
The Fast Sieve of Eratosthenes.
A sequential and optimized version of the sieve of Eratosthenes.
The program calculates a list of the first NrOfPrime primes.
The result of the program is the NrOfPrimes'th prime.
Strictness annotations have been added because the strictness analyser
is not able to deduce all strictness information. Removal of these !'s
will make the program about 20% slower.
>>
IMPORT deltaI, deltaR;
MACRO
NrOfPrimes -> 3000;
RULE
== The sieve algorithm: generate an infinite list of all primes.
:: Primes -> [INT];
Primes -> pr:[5 | Sieve 7 4 pr];
:: Sieve INT !INT [INT] -> [INT];
Sieve g i prs
-> [g | Sieve' g i prs] , IF IsPrime prs g (RTOI (SQRT (ITOR g)))
-> Sieve (+ g i) (- 6 i) prs;
:: Sieve' INT INT [INT] -> [INT];
Sieve' g i prs -> Sieve (+ g i) (- 6 i) prs;
:: IsPrime [INT] !INT INT -> BOOL;
IsPrime [f|r] pr bd -> TRUE , IF > f bd
-> FALSE , IF = (% pr f) 0
-> IsPrime r pr bd;
== Select is used to get the NrOfPrimes'th prime from the infinite list.
:: Select [x] INT -> x;
Select [f|r] 1 -> f;
Select [f|r] n -> Select r (-- n);
<< The Start rule: Select the NrOfPrimes'th prime from the list of primes
generated by Primes.
>>
:: Start -> INT;
Start -> Select [2, 3 | Primes] NrOfPrimes;